<?php

class Bizprofile_Widgets_Info_Widget extends Tomato_Widget 
{
	protected function _prepareShow() 
	{	
		$enterprise_id = 0;
		$user_id = 0;
		$auth = Zend_Auth::getInstance();
		$enterprise = null;
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$lang = Zend_Controller_Front::getInstance()->getRequest()->getParam('lang');
		$this->_view->assign('lang', $lang);
		if ($auth->hasIdentity()) {
			$user = $auth->getIdentity();
			if($user->enterprise){
				$enterprise_id = $user->enterprise->id;
				$user_id = $user->user_id;
				$enterprise = $user->enterprise;
			}
		}
				
		$params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
		//print_r($params);
		if($params['module'] == 'bizprofile' && $params['controller'] == 'Enterprise' && $params['action'] == 'index' && isset($params['enterprise_id'])){
			$enterprise_id = $params['enterprise_id'];				
			$enterpriseDao  = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
			$enterpriseDao->setDbConnection($conn);			
			$enterprise = $enterpriseDao->getById($enterprise_id);			
		}
		
		if($enterprise_id > 0 && isset($enterprise) && $enterprise->frontend_active == 1){
			$this->_view->assign('enterprise', $enterprise);
			
			$infoDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getInfoDao();
			$infoDao->setDbConnection($conn);				
			$infoDao->setLang($lang);				
			$infos = $infoDao->getInfos($enterprise_id, 4);
			$this->_view->assign('infos', $infos);
			if($lang == 'en_US'){
				$infoDao->setLang('vi_VN');				
				$vi_infos = $infoDao->getInfos($enterprise_id, 4);
				$this->_view->assign('vi_infos', $vi_infos);
			}
			
			$permissionDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPermissionDao();
			$permissionDao->setDbConnection($conn);
			//$user_entity = $permissionDao->getUserEntity($user_id,$enterprise_id);
			/** Khanh
	        * Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
	        * $user_entity = 1 : User chua login
	        * $user_entity = 2 : User da login, nhung ko la DN
	        * $user_entity = 3 : User da login, la DN khach
	        * $user_entity = 4 : User da login, la DN doi tac
	        **/
			if(isset($params['enterprise_id']))
			{
				//$auth = Zend_Auth::getInstance();
		        if ($auth->hasIdentity()) 
		        { 
		            $user = $auth->getIdentity();
		            if(!isset($user->enterprise->id)){
			        	$user_entity = 2; // user da login, nhung ko phai la DN
			        }
			        else
			        {
			        	$partnerDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPartnerDao();
		                $partnerDao->setDbConnection($conn);
		
		                $pageIndex     = 1;
		                $perPage = 5;
		                $offset     = ($pageIndex - 1) * $perPage;
		                $exp = array(
		                    'relationid' => $params['enterprise_id'],
		                    'partner_type' => 'buddy',
		                    'partner_status' => 'yes',
							'deleted'		 => 0
		                );
						
		                //* In ralationship
		                $partners = $partnerDao->find($offset, $perPage, $exp);
		                $numPartners   = $partnerDao->count($exp);
		                
		                $user_entity = 3;// DN khach
		                $request = $this->getRequest();
		                foreach($partners as $row):
							$expRecomment = array(
								'relationid' => $row->relationid,
								'enterprise_id' => $row->enterprise_id, // DN doi tac
							);
							if($user->enterprise->id == $row->enterprise_id)
							{
								$user_entity = 4; // DN doi tac
							}
						endforeach;
			        }
		        }
		        else 
		        {
		        	$user_entity = 1; // user chua login 
		        }
			}
			/**
		`	* END : Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
		    **/
			else 
			{
				$user_entity = $permissionDao->getUserEntity($user_id,$enterprise_id);
			}
			if ($auth->hasIdentity()) { 
	            $user = $auth->getIdentity();
				if($user->enterprise){
					if($user->enterprise->id == $enterprise_id) // user đã login thuộc DN đang xem
			    	{
			    		$permission_viewall = 1;
			    		$this->_view->assign('permission_viewall', $permission_viewall);
			    	}
				}
        	}
			$this->_view->assign('user_entity', $user_entity);
			
			$permissionDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPermissionDao();
			$permissionDao->setDbConnection($conn);
			$permission = $permissionDao->getPermissionByUserEntity($enterprise_id, $user_entity);
			if($permission != null){
				$permission->enterprise_info = unserialize($permission->enterprise_info);			
				$this->_view->assign('permission', $permission);
			}			
		}
	}
	
	protected function _prepareConfig()
	{
		
	}
}
